﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace WP7iPhone
{
    public partial class LaunchTask : PhoneApplicationPage
    {
        public LaunchTask()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (!State.ContainsKey("launching"))
            {
                State["launching"] = false;
                
            }
            if ((bool)State["launching"] == true)
            {
                State["launching"] = false;
                NavigationService.GoBack();
                return;
            }
            //
            // Our query string tells us what task to launch
            //
            string task = NavigationContext.QueryString["task"];
            switch (task)
            {
                case "camera":
                    CameraCaptureTask camera = new CameraCaptureTask();
                    camera.Show();
                    break;

                case "mail":
                    EmailComposeTask mail = new EmailComposeTask();
                    mail.Show();
                    break;

                case "phone":
                    PhoneCallTask phone = new PhoneCallTask();
                    phone.Show();
                    break;

                case "web":                    
                    WebBrowserTask web = new WebBrowserTask();
                    web.Show();
                    break;

                case "media":
                    //MediaPlayerLauncher media = new MediaPlayerLauncher();
                    //media.Show();
                    // this doesn't work, we need to have a specific media URL that we want to play
                    // there is no way to launch the media app library and let the user pick.
                    break;

                case "photos":
                    PhotoChooserTask photos = new PhotoChooserTask();
                    photos.Show();
                    break;

                case "appstore":
                    MarketplaceHubTask appstore = new MarketplaceHubTask();
                    appstore.ContentType = MarketplaceContentType.Applications;
                    appstore.Show();
                    break;

                case "itunes":
                    MarketplaceHubTask itunes = new MarketplaceHubTask();
                    itunes.ContentType = MarketplaceContentType.Music;
                    itunes.Show();
                    break;

                case "contacts":
                    EmailAddressChooserTask email  = new EmailAddressChooserTask();
                    email.Show();
                    break;
                default:
                    throw new NotImplementedException();
            }
            State["launching"] = true;
        }
    }
}